/*-------------------------------------------------------------------------
 *
 * testint128.c
 *	  Testbed for roll-our-own 128-bit integer arithmetic.
 *
 * This is a standalone test program that compares the behavior of an
 * implementation in int128.h to an (assumed correct) int128 native type.
 *
 * Copyright (c) 2017-2022, PostgreSQL Global Development Group
 *
 *
 * IDENTIFICATION
 *	  src/tools/testint128.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres_fe.h"

/*
 * By default, we test the non-native implementation in int128.h; but
 * by predefining USE_NATIVE_INT128 to 1, you can test the native
 * implementation, just to be sure.
 */
#ifndef USE_NATIVE_INT128
#define USE_NATIVE_INT128 0
#endif

#include "common/int128.h"
#include "common/pg_prng.h"

/*
 * We assume the parts of this union are laid out compatibly.
 */
typedef union
{
	int128		i128;
	INT128		I128;
	union
	{
#ifdef WORDS_BIGENDIAN
		int64		hi;
		uint64		lo;
#else
		uint64		lo;
		int64		hi;
#endif
	}			hl;
}			test128;


/*
 * Control version of comparator.
 */
static inline int fc_my_int128_compare(int128 fc_x, int128 fc_y)
{
	if (fc_x < fc_y)
		return -1;
	if (fc_x > fc_y)
		return 1;
	return 0;
}

/*
 * Main program.
 *
 * Generates a lot of random numbers and tests the implementation for each.
 * The results should be reproducible, since we use a fixed PRNG seed.
 *
 * You can give a loop count if you don't like the default 1B iterations.
 */
int main(int fc_argc, char **fc_argv)
{
	long		fc_count;

	pg_prng_seed(&pg_global_prng_state, 0);

	if (fc_argc >= 2)
		fc_count = strtol(fc_argv[1], NULL, 0);
	else
		fc_count = 1000000000;

	while (fc_count-- > 0)
	{
		int64		fc_x = pg_prng_uint64(&pg_global_prng_state);
		int64		fc_y = pg_prng_uint64(&pg_global_prng_state);
		int64		fc_z = pg_prng_uint64(&pg_global_prng_state);
		test128		fc_t1;
		test128		fc_t2;

		/* check unsigned addition */
		fc_t1.hl.hi = fc_x;
		fc_t1.hl.lo = fc_y;
		fc_t2 = fc_t1;
		fc_t1.i128 += (int128) (uint64) fc_z;
		int128_add_uint64(&fc_t2.I128, (uint64) fc_z);

		if (fc_t1.hl.hi != fc_t2.hl.hi || fc_t1.hl.lo != fc_t2.hl.lo)
		{
			printf("%016lX%016lX + unsigned %lX\n", fc_x, fc_y, fc_z);
			printf("native = %016lX%016lX\n", fc_t1.hl.hi, fc_t1.hl.lo);
			printf("result = %016lX%016lX\n", fc_t2.hl.hi, fc_t2.hl.lo);
			return 1;
		}

		/* check signed addition */
		fc_t1.hl.hi = fc_x;
		fc_t1.hl.lo = fc_y;
		fc_t2 = fc_t1;
		fc_t1.i128 += (int128) fc_z;
		int128_add_int64(&fc_t2.I128, fc_z);

		if (fc_t1.hl.hi != fc_t2.hl.hi || fc_t1.hl.lo != fc_t2.hl.lo)
		{
			printf("%016lX%016lX + signed %lX\n", fc_x, fc_y, fc_z);
			printf("native = %016lX%016lX\n", fc_t1.hl.hi, fc_t1.hl.lo);
			printf("result = %016lX%016lX\n", fc_t2.hl.hi, fc_t2.hl.lo);
			return 1;
		}

		/* check multiplication */
		fc_t1.i128 = (int128) fc_x * (int128) fc_y;

		fc_t2.hl.hi = fc_t2.hl.lo = 0;
		int128_add_int64_mul_int64(&fc_t2.I128, fc_x, fc_y);

		if (fc_t1.hl.hi != fc_t2.hl.hi || fc_t1.hl.lo != fc_t2.hl.lo)
		{
			printf("%lX * %lX\n", fc_x, fc_y);
			printf("native = %016lX%016lX\n", fc_t1.hl.hi, fc_t1.hl.lo);
			printf("result = %016lX%016lX\n", fc_t2.hl.hi, fc_t2.hl.lo);
			return 1;
		}

		/* check comparison */
		fc_t1.hl.hi = fc_x;
		fc_t1.hl.lo = fc_y;
		fc_t2.hl.hi = fc_z;
		fc_t2.hl.lo = pg_prng_uint64(&pg_global_prng_state);

		if (fc_my_int128_compare(fc_t1.i128, fc_t2.i128) !=
			int128_compare(fc_t1.I128, fc_t2.I128))
		{
			printf("comparison failure: %d vs %d\n",
				   fc_my_int128_compare(fc_t1.i128, fc_t2.i128),
				   int128_compare(fc_t1.I128, fc_t2.I128));
			printf("arg1 = %016lX%016lX\n", fc_t1.hl.hi, fc_t1.hl.lo);
			printf("arg2 = %016lX%016lX\n", fc_t2.hl.hi, fc_t2.hl.lo);
			return 1;
		}

		/* check case with identical hi parts; above will hardly ever hit it */
		fc_t2.hl.hi = fc_x;

		if (fc_my_int128_compare(fc_t1.i128, fc_t2.i128) !=
			int128_compare(fc_t1.I128, fc_t2.I128))
		{
			printf("comparison failure: %d vs %d\n",
				   fc_my_int128_compare(fc_t1.i128, fc_t2.i128),
				   int128_compare(fc_t1.I128, fc_t2.I128));
			printf("arg1 = %016lX%016lX\n", fc_t1.hl.hi, fc_t1.hl.lo);
			printf("arg2 = %016lX%016lX\n", fc_t2.hl.hi, fc_t2.hl.lo);
			return 1;
		}
	}

	return 0;
}
